import Versions from './components/Versions'
import electronLogo from './assets/electron.svg'
import { useEffect, useState } from 'react'
import { Button, message } from 'antd'

function App() {
  // 串口开关标识
  const [serialPortFlag, setSerialPortFlag] = useState(false)

  // 主进程消息接收函数
  const onMainMessage = () => {
    // 打开串口
    window.electron.ipcRenderer.on('SerialPort-openSerialPort', (event, arg) => {
      if (arg) {
        message.error('串口拒绝访问,或串口被占用')
      } else {
        message.success('串口打开成功')
      }
    })

    // 获取串口列表
    window.electron.ipcRenderer.on('SerialPort-getList', (event, arg) => {
      console.log(arg)
    })
  }

  // 获取串口列表
  const getSerialPortList = () => {
    window.electron.ipcRenderer.send('getSerialPortList')
  }

  // 打开串口
  const openSerialPort = () => {
    let comOptions = {
      path: 'COM4',
      baudRate: 9600
    }
    window.electron.ipcRenderer.send('openSerialPort', comOptions)
  }

  // 关闭串口

  useEffect(() => {
    getSerialPortList()

    // 打开消息接收
    onMainMessage()
  }, [])

  return (
    <>
      <Button onClick={openSerialPort}>打开串口</Button>
    </>
  )
}

export default App
